﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace ath2011_sql_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    
    public partial class MainWindow : Window
    {

        
        string[] autocomplete = new string[]{ "SELECT ProductName, ProductName2, Product3 FROM Products WHERE ProductName = 'cos'",
                                        "SELECT ProductName FROM Products",
                                        "SELECT * FROM tblContacts",
                                        "INSERT INTO t1 (a1,b1) VALUES (3 , 12)",
                                        "UPDATE t1 SET a1=3, b1=12 WHERE a1=2",
                                        "DELETE t1 WHERE a1=10"};
        
        string conStr = "connection";
        FormSettings FS;

        public MainWindow()
        {
            InitializeComponent();
             FS = new FormSettings();
            conStr = FS.txtCnStr.Text;
        }

        void colorText(string r, RichTextBox MyTextInput, Color c)   ///// source: http://stackoverflow.com/questions/5685644/c-sharp-wpf-highlight-text-in-richtextbox
        { 
           // TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
           // range.Text = @"TOP a multiline text or file END";
            Regex reg = new Regex(r, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            var start = MyTextInput.Document.ContentStart;
            while (start != null && start.CompareTo(MyTextInput.Document.ContentEnd) < 0)
            {
                if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    var match = reg.Match(start.GetTextInRun(LogicalDirection.Forward));

                    var textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
                    textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(c));
                    //textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    start = textrange.End; // I'm not sure if this is correct or skips ahead too far, try it out!!!
                }
                start = start.GetNextContextPosition(LogicalDirection.Forward);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            FS = new FormSettings();
            FS.Show();

           // var newWindow = new FormSettings();
           // newWindow.Show();
        }

        private string addText(string[] temp)
        {
            string temp1="";
            for (int i = 0; i < temp.Length; i++)
			{
                temp1 += (temp[i] + "\r\n");
			}
            return temp1;
        }

        private void btnGenerate_Click(object sender, RoutedEventArgs e)
        {
            FS = new FormSettings();
            conStr = FS.txtCnStr.Text;
            string textAdonet="";
            string textDataset="";
            string textLinq = "";
            string textSP = "";
            string textLinqSP = "";

            sql_to_code sq1 = new sql_to_code(wyszukaj.Text, 0); // - Ado.Net
            string[] csharp_code = sq1.convert();
            textAdonet = addText(csharp_code);
            
            sql_to_code sq2 = new sql_to_code(wyszukaj.Text, 1); // - DataSet
            string[] csharp_code1 = sq2.convert();
            textDataset = addText(csharp_code1);

            sql_to_code sq3 = new sql_to_code(wyszukaj.Text, 2); // - Linq
            string[] csharp_code2 = sq3.convert();
            textLinq = addText(csharp_code2);

            sql_to_code sq4 = new sql_to_code(wyszukaj.Text, 3); // - Stored Procedures
            string[] csharp_code3 = sq4.convert();
            textSP = addText(csharp_code3);

            sql_to_code sq5 = new sql_to_code(wyszukaj.Text, 4); // - Stored Procedures
            string[] csharp_code4 = sq5.convert();
            textLinqSP = addText(csharp_code4);

            if (chkResults.IsChecked == true)
            {
                results r1 = new results();
                string[] res1 = r1.ShowResults(conStr, wyszukaj.Text);

                r1 FR = new r1(res1, conStr, wyszukaj.Text,sq1.subType);
                FR.Show();
            }

            ////////////////////// Coloring generated c# code ///////////////////////////
            FlowDocument myFlowDoc = new FlowDocument();
            myFlowDoc.Blocks.Add(new Paragraph(new Run(textAdonet)));
            rtAdonet.Document = myFlowDoc;

            myFlowDoc = new FlowDocument();
            myFlowDoc.Blocks.Add(new Paragraph(new Run(textDataset)));
            rtDataset.Document = myFlowDoc;

            myFlowDoc = new FlowDocument();
            myFlowDoc.Blocks.Add(new Paragraph(new Run(textLinq)));
            rtLinq.Document = myFlowDoc;

            myFlowDoc = new FlowDocument();
            myFlowDoc.Blocks.Add(new Paragraph(new Run(textSP)));
            rtStorProc.Document = myFlowDoc;

            myFlowDoc = new FlowDocument();
            myFlowDoc.Blocks.Add(new Paragraph(new Run(textLinqSP)));
            rtLinqSP.Document = myFlowDoc;

            string s1 = @"\s+(Foreach|For|Next|If|Then|new|int|float|string|true|false|try|catch|var|from|in|where|select)";
            string s2 = @"\s+(DataSet|DataRow|DataGrid|SqlConnection|SqlDataAdapter|SqlCommand|DataTable)";
            string s3 = @"""[^""\\]*(?:\\.[^""\\]*)*""";

          colorText(s1, rtAdonet, Colors.Blue);
          colorText(s2, rtAdonet, Colors.Turquoise);
          colorText(s3, rtAdonet, Colors.IndianRed);

          colorText(s1, rtDataset, Colors.Blue);
          colorText(s2, rtDataset, Colors.Turquoise);
          colorText(s3, rtDataset, Colors.IndianRed);

          colorText(s1, rtLinq, Colors.Blue);
          colorText(s2, rtLinq, Colors.Turquoise);
          colorText(s3, rtLinq, Colors.IndianRed);

          colorText(s1, rtStorProc, Colors.Blue);
          colorText(s2, rtStorProc, Colors.Turquoise);
          colorText(s3, rtStorProc, Colors.IndianRed);

          colorText(s1, rtLinqSP, Colors.Blue);
          colorText(s2, rtLinqSP, Colors.Turquoise);
          colorText(s3, rtLinqSP, Colors.IndianRed);
        }

        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        private void wyszukaj_TextChanged(object sender, TextChangedEventArgs e)
        {
            //wyszukaj.Text = autocomplete[0];
            listAutoC.Items.Clear();



            for (int i = 0; i < autocomplete.Length; i++)
            {
                string temp = wyszukaj.Text;
                string temp2 = autocomplete[i];

                if (temp2.StartsWith(temp, StringComparison.OrdinalIgnoreCase) && temp != temp2) { listAutoC.Visibility = Visibility.Visible; listAutoC.Items.Add(autocomplete[i]); }
            }
        }

        private void listAutoC_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            for (int i = 0; i < listAutoC.Items.Count; i++)
            {
                listAutoC.Visibility = Visibility.Hidden;
                wyszukaj.Text = listAutoC.SelectedValue.ToString();
            }
            
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            listAutoC.Visibility = Visibility.Hidden;
        }

        private void btnTest_Click(object sender, RoutedEventArgs e)
        {
            conStr = FS.txtCnStr.Text;
            FormTest ft1= new FormTest(conStr);
            ft1.Show();
        }
    }
}
